package com.turing.data.scripts.contract.filter


import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.data.engine.utils.RegexExtract

class Duration_30190_10_filter {


    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX = /.*存续期.*/
        String REGEX_1 = /.*有效期限.*/

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        def set = new HashSet()
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 10 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX || expandString =~ REGEX_1 ){
                        trueValueSet.add(drawResultVO)
                        set.add(drawResultVO.text)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        try{
            if(trueValueSet.size()==0){

                String REGEX_RULE = /存续期.{1,20}\d.{1,5}年/
                println dependParams.JSONTextMap.size()
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX_RULE,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )

                if(trueValueSet.size()>0){
                    def textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)
                    def rawPathRule = textPathMap.get(childTaskSn)
                    def rawTextRule = FileUtils.loadJson(rawPathRule)
                    def text = trueValueSet[0].text
                    def startOffset = text.indexOf("为")+"为".length()
                    if(startOffset>-1){
                        def endOffset = 0
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawTextRule)
                        trueValueSet.clear()
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }else{
                        return trueValueSet
                    }
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

}
